package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.approve;

import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.approve.ApproveBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;
import java.io.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/approve")
public class ApproveController {
    @Autowired
    private ApproveBu approveBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;

    @GetMapping("/getApprove")
    public SzResult getApprove(@RequestParam(required = false) String approveExplain, HttpServletRequest request) throws Exception {
        String plantId = request.getHeader("Plant-Id");
        return approveBu.getApprove(approveExplain, plantId);
    }

    //   获取所有的审批类型,不带未启用的
//    public SzResult getApproveIsOn(String approveExplain, String plantId) throws Exception {
    @GetMapping("/getApproveIfOn")
    public SzResult getApproveIsOn(@RequestParam(required = false) String approveExplain, HttpServletRequest request) throws Exception {
        String plantId = request.getHeader("Plant-Id");
        return approveBu.getApproveIsOn(approveExplain, plantId);
    }
    @PostMapping("/addApprove")
    @PermissionCheck("addApprove")
    public SzResult addApprove(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String approveName = jsonObject.getString("approveName");
        String approveExplain = jsonObject.getString("approveExplain");
        String templateId = jsonObject.getString("templateId");
        Integer approveTier = jsonObject.getInteger("approveTier");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.addApprove(approveName, approveExplain, approveTier,templateId, token, plantId);
    }

    @PostMapping("/updateApprove")
    @PermissionCheck("updateApprove")
    public SzResult updateApprove(@RequestBody JSONObject jsonObject,
                                  HttpServletRequest request
    ) throws Exception {

//        String id,Integer approveTier,Integer ifOn,String token
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");
        Integer approveTier = jsonObject.getInteger("approveTier");
        Integer ifOn = jsonObject.getInteger("ifOn");
        String templateId = jsonObject.getString("templateId");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.updateApprove(id, approveTier, ifOn,templateId, token, plantId);
    }

    @PostMapping("/deleteUserApprove")
    @PermissionCheck("deleteUserApprove")
    public SzResult deleteUserApprove(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.deleteUserApprove(token, id, plantId);
    }

    @GetMapping("/getUserApprove")
    public SzResult getUserApprove(@RequestParam String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.getUserApprove(id, token, plantId);
    }

    @PostMapping("/addUserApprove")
    @PermissionCheck("addUserApprove")
    public SzResult addUserApprove(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String userId = jsonObject.getString("userId");
        String approveId = jsonObject.getString("approveId");
        Integer approveTier = jsonObject.getInteger("approveTier");
        String branch = jsonObject.getString("branch");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.addUserApprove(token, userId, approveId, approveTier, branch ,plantId);
    }

    //    HttpServletRequest request
    @GetMapping("/getApproveInfo")
    public SzResult getApproveInfo(@RequestParam(value = "page", required = false) Integer page,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime,
                                   @RequestParam(value = "approveId", required = false) String approveId,
                                   @RequestParam(value = "approveState", required = false) Integer approveState,
                                   @RequestParam(value = "approveInfo", required = false) String approveInfo,
                                   HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.getApproveInfo(page, pageSize, startTime, endTime, approveId, approveState,approveInfo, token, plantId);
    }

    @GetMapping("/getApproveInfoDetail")
    public SzResult getApproveInfoDetail(@RequestParam String approveInfoId, HttpServletRequest request) throws Exception {
        String plantId = request.getHeader("Plant-Id");
        return approveBu.getApproveInfoDetail(approveInfoId, plantId);
    }
    //上传文件
    @PostMapping("/upload")
//    @PermissionCheck("approve_file_upload")
//    @PermissionCheck("file_upload")
    public SzResult uploadFile(@RequestParam("file") MultipartFile file,
                           @RequestParam(required = false) String id, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        String token = request.getHeader("Authorization");
        return approveBu.uploadInfoFile(file, id, token);
    }
    @PostMapping("/download")
    public void downloadFile(@RequestBody JSONObject jsonObject, HttpServletResponse response) throws IOException {
        String id = jsonObject.getString("id");
        approveBu.downloadInfoFile(id,response);
    }
    //    删除文件接口
    @PostMapping("/deleteFile")
    public SzResult deleteFile(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        try {
            String id = jsonObject.getString("id");
            return approveBu.deleteInfoFile(id);
        } catch (Exception e) {
            e.printStackTrace();
            return SzResult.error(e.getMessage());
        }
    }
    @PostMapping("/uploadAttachment")
    public SzResult uploadAttachment(@RequestParam("file") MultipartFile file,
                               @RequestParam(required = false) String id, HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        String token = request.getHeader("Authorization");
        return approveBu.uploadInfoFile(file, id+"1", token);
    }
    @PostMapping("/downloadAttachment")
    public void downloadAttachment(@RequestBody JSONObject jsonObject, HttpServletResponse response) throws IOException {
        String id = jsonObject.getString("id")+"1";
        approveBu.downloadInfoFile(id,response);
    }
    @PostMapping("/deleteAttachment")
    public SzResult deleteAttachment(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        try {
            String id = jsonObject.getString("id")+"1";
            return approveBu.deleteInfoFile(id);
        } catch (Exception e) {
            e.printStackTrace();
            return SzResult.error(e.getMessage());
        }
    }
//    @GetMapping("/deleteUserApproveByApproveId")
//    public SzResult deleteUserApproveByApproveId( @RequestParam String id,HttpServletRequest request) throws Exception {
//        String token = request.getHeader("Authorization");
//        return approveBu.deleteUserApproveByApproveId(token, id);
//    }

    @PostMapping("/approvalProcess")
    public SzResult approvalProcess(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String remark = jsonObject.getString("remark");
        String id = jsonObject.getString("id");
        Integer state = jsonObject.getInteger("state");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.approvalProcess(token, id, remark, state, plantId);
    }

//    @PostMapping("/approvalProcessFail")
//    public SzResult approvalProcessFail( @RequestBody JSONObject jsonObject,HttpServletRequest request) throws Exception {
//        String approveId = jsonObject.getString("approveId");
//        String remark = jsonObject.getString("remark");
//        String token = request.getHeader("Authorization");
//        return approveBu.approvalProcessFail(token, approveId, remark);
//    }

    @PostMapping("/updateApprovalProcessDetail")
    public SzResult updateApprovalProcessDetail(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
//        String token,String remark, String id, Integer approveState
        String remark = jsonObject.getString("remark");
        String id = jsonObject.getString("id");
        Integer approveState = jsonObject.getInteger("approveState");
        return approveBu.updateApprovalProcessDetail(token, remark, id, approveState);
    }


    @GetMapping("/deleteApproval")
    public SzResult deleteApproval(@RequestParam String id, @RequestParam String remark, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.deleteApproval(token, id, remark, plantId);
    }


    @GetMapping("/revokeApprovalProcess")
    public SzResult approvalProcess(@RequestParam String id, @RequestParam String remark, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.revokeApprovalProcess(token, id, remark, plantId);
    }


    @GetMapping("/myApprove")
    public SzResult myApprove(
//            Integer page,Integer pageSize,String token,Integer approveState,String approveId
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "approveState", required = false) Integer approveState,
            @RequestParam(value = "approveId", required = false) String approveId,
            HttpServletRequest request
    ) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.myApprove(page, pageSize, token, approveState, approveId, plantId);
    }

    //    修改审批信息备注
    @PostMapping("/updateApproveInfo")
    public SzResult updateApproveInfo(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");
        String remark = jsonObject.getString("remark");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.updateApproveInfo(token, id, remark, plantId);
    }


    @PostMapping("/approveUser")
    public SzResult approveUser(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String approveId = jsonObject.getString("approveId");
        String plantId = request.getHeader("Plant-Id");
        return approveBu.approveUser(approveId, token, plantId);
    }
    //添加审批单
    @PostMapping("/addApproveInfo")
    @PermissionCheck("addApproveInfo")
    public SzResult addApproveInfo(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String approveId = jsonObject.getString("approveId");
        String title = jsonObject.getString("title");
        return approveBu.addApproveInfo(token, plantId,approveId,title,jsonObject);
    }

    //修改审批单
    @PostMapping("/updateApproveInfoDate")
    @PermissionCheck("updateApproveInfoDate")
    public SzResult updateApproveInfoDate(@RequestBody JSONObject jsonObject) throws Exception {
        String id = jsonObject.getString("id");
        Map<String, Object> map = jsonObject.getInnerMap();
        map.remove("id");
        return approveBu.updateApproveInfoDate(id,map);
    }

//    删除审批单
    @PostMapping("/deleteApproveInfo")
    @PermissionCheck("deleteApproveInfo")
    public SzResult deleteApproveInfo(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");
        return approveBu.deleteApproveInfo(id);
    }

    //审批单表格详情
    @PostMapping("/getApproveInfoExcel")
    public SzResult getApproveInfoExcel(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");
        return approveBu.getApproveInfoExcel(id);
    }
    //审批模板
    @GetMapping("/getApproveTemplate")
    public SzResult getApproveTemplate() throws Exception {
        return approveBu.getApproveTemplate();
    }

    //删除审批类型
//    public SzResult deleteApprove(String id, String plantId, String token) throws Exception {
    @PostMapping("/deleteApprove")
    public SzResult deleteApprove(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");
        return approveBu.deleteApprove(id, token,plantId);
    }
}


